using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LAFDC.DataAccess;
using LAFDC.Web.UI;
using LAFDC.Logging;

namespace LAFDC.Web.UI {
    public partial class AdherentAjout : System.Web.UI.Page {
        private readonly string source = "AdhAjout.aspx";
        protected void Page_Load(object sender, EventArgs e) {
            // txtADHESION.Text = DateTime.Today.Date.ToShortDateString();
        }

        protected void Submit_Click(object sender, EventArgs e) {
            FDCLogger.LogEvent("L'utilisateur a soumit la forme au serveur", source, 3);

            string insertAdherent = string.Empty;
            string insertEtatCivile = string.Empty;
            string insertAdresse = string.Empty;
            string insertDivers = string.Empty;

            if (this.IsPostBack) {
                foreach (string changedObject in FormChangeTracker.mChangedObjects) {
                    FDCLogger.LogEvent("Le formulaire contient des information dans le champ " + changedObject, source, 4);
                }

                string PREV_ID = txtPREV_ID.Text;

                // RETRIEVE DATA FROM WEB-FORM

                #region// CREATE INSERT ADHERENT CMD
                string Asso = listASSO.Text;
                string Section = listSECTION.SelectedValue;
                string Retenu = string.Empty;
                if (chkRETENU.Checked) { Retenu = "OUI"; } else { Retenu = "NON"; }
                string Adhesion = txtADHESION.Text;

                insertAdherent = @"insert adherent (prev_id, ad, section, retenu, date_adhesion) values ('" + PREV_ID + "','" + Asso + "','" + Section + "','" + Retenu + "','" + Adhesion + "')";

                #endregion

                #region // CREATE INSERT ETAT CIVILE CMD
                string Nom = txtNOM.Text;
                string Prenom = txtPRENOM.Text;
                string Intitule = listIntitule.SelectedValue;
                string SitFamille = listSituationFamille.SelectedValue;
                string Anniv = txtANNIV.Text;

                insertEtatCivile = @"insert personne (prev_id, nom, prenom, [civ-sexe], sfam, anniv) values ('" + PREV_ID + "','" + Nom + "','" + Prenom + "','" + Intitule + "','" + SitFamille + "','" + Anniv + "')";

                #endregion

                #region// CREATE INSERT ADRESSE CMD
                string Addr1 = txtAdr1.Text;
                string Addr2 = txtAdr2.Text;
                string CodePostal = txtCodePostal.Text;
                string Ville = txtVille.Text;

                insertAdresse = @"insert adresse (prev_id, adresse_1, adresse_2, codepostal, ville, pays) values ('" + PREV_ID + "','" + Addr1 + "','" + Addr2 + "','" + CodePostal + "','" + Ville + "','France')";

                #endregion

                #region // CREATE INSERT DIVERS CMD
                string Affectation = txtAFFECTATION.Text;
                string IRET = txtIRET.Text;
                string OTIG = txtOTIG.Text;

                insertDivers = @"insert divers (prev_id, affectation, indice_retraite, otig) values ('" + PREV_ID + "','" + Affectation + "','" + IRET + "','" + OTIG + "')";

                #endregion

                try {
                    ConnectionManager cn = new ConnectionManager();
                    SqlConnection conn = cn.GetSqlConnection();
                    SqlCommand tsql = conn.CreateCommand();

                    tsql.CommandType = CommandType.Text;
                    conn.Open();

                    #region // START TRANSACTION
                    tsql.CommandText = "BEGIN TRAN";
                    int result = tsql.ExecuteNonQuery();
                    FDCLogger.LogEvent(tsql.CommandText + ", Result = " + result.ToString(), source, 3);
                    #endregion

                    // Insert ADHERENT
                    tsql.CommandText = insertAdherent;
                    result = tsql.ExecuteNonQuery();
                    FDCLogger.LogEvent(tsql.CommandText + ", Result = " + result.ToString(), source, 3);
                    // Insert ETAT_CIVILE
                    tsql.CommandText = insertEtatCivile;
                    result = tsql.ExecuteNonQuery();
                    FDCLogger.LogEvent(tsql.CommandText + ", Result = " + result.ToString(), source, 3);

                    // Insert ADRESSE
                    tsql.CommandText = insertAdresse;
                    result = tsql.ExecuteNonQuery();
                    FDCLogger.LogEvent(tsql.CommandText + ", Result = " + result.ToString(), source, 3);

                    // Insert DIVERS
                    tsql.CommandText = insertDivers;
                    result = tsql.ExecuteNonQuery();
                    FDCLogger.LogEvent(tsql.CommandText + ", Result = " + result.ToString(), source, 3);

                    #region // COMMIT TRANSACTION
                    tsql.CommandText = "COMMIT TRAN;";
                    result = tsql.ExecuteNonQuery();
                    FDCLogger.LogEvent(tsql.CommandText + ", Result = " + result.ToString(), source, 3);
                    #endregion
                    conn.Close();
                }
                catch (Exception ex) {
                    FDCLogger.LogEvent(ex.Message, ex.Source, 1);
                    //Response.Redirect("Erreur.aspx");
                }

                if ((PREV_ID != null) && (PREV_ID.Length != 0)) {
                    Response.Redirect("AdhConsultation.aspx?adh=" + PREV_ID);
                }

            }
        }

        #region // Change Tracking events

        #region // ADRESSE
        protected void txtAdr1_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("ADRESSE");
        }

        protected void txtAdr2_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("ADRESSE");
        }

        protected void txtCodePostal_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("ADRESSE");
        }

        protected void txtVille_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("ADRESSE");
        }
        #endregion

        #region // DIVERS
        protected void txtOTIG_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("DIVERS");
        }

        protected void txtIRET_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("DIVERS");
        }

        protected void txtAFFECTATION_TextChanged(object sender, EventArgs e) {
            FormChangeTracker tracker = new FormChangeTracker();
            tracker.RegisterChange("DIVERS");
        }
        #endregion
        #endregion
    }
}
